import router from './router'
import store from './store'
import {
  Message
} from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import {
  getToken
} from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
const _import = require('./router/_import_' + process.env.NODE_ENV) // 获取组件的方法
import Layout from '@/layout'
import de from 'element-ui/src/locale/lang/de'
NProgress.configure({
  showSpinner: false
}) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start() // 开启进度条

  // set page title
  document.title = getPageTitle(to.meta.title) // 设置网页标题

  // determine whether the user has logged in
  const hasToken = getToken() // 获取cookie中的token
  // 如果 hasToken  ！= ’‘
  if (hasToken) {
    // if 去的path 是登陆页
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({
        path: '/'
      }) // 正常执行，到 /
      NProgress.done() // 关闭进度条
    } else { // 如果去的不是登陆页面
      const hasGetUserInfo = store.getters.username // 获取vuex中的name
      console.log('>>>>>>>', hasGetUserInfo)
      if (hasGetUserInfo) {
        next() // 如果获取到vuex中的name 就正常执行
      } else {
        // 如果没有获取到vuex中到name
        try {
          // get user info

          await store.dispatch('user/getInfo') // 触发获取用户信息到接口
          await store.dispatch('user/getRouter') // 触发获取路由表的接口
          console.log('vuex的路由==>', store.getters.menus)
          const menusArr = []
          generaMenu(menusArr, store.getters.menus) // 拼装动态路由

          if (store.getters.menus.length < 1) {
            global.antRouter = []
            next()
          }
          // const menus = filterAsyncRouter(menusArr) // 过滤路由
          console.log('动态路由数据====》', menusArr)
          router.addRoutes(menusArr) // 动态添加路由

          global.antRouter = menusArr // 将路由数据传递给全局变量，做侧边栏渲染的工作
          // next() //正常走
          next({
            ...to,
            replace: true
          })
        } catch (error) {
          // remove token and go to login page to re-login
          await store.dispatch('user/resetToken') // 触发vuex中  resetToken
          console.log('这是找不到组件报错，element-ui的message报错', error)
          Message.error(error.toString() || 'Has Error') // 弹出异常

          next(`/login?redirect=${to.path}`) // 然后就执行这里 跳转到 login  redirect把从哪个页面出错的
          NProgress.done()
        }
      }
    }
  } else {
    /* has no token*/

    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})

// 路由拼装
function generaMenu(routes, data) {
  data.forEach(item => {
    if (item.component) {
      if (item.component === 'Layout') {
        item.component = Layout
      } else {
        item.component = _import(item.component) // 导入组件
      }
    }
    let name = item.route
    if(name)
      name = item.route.trim().toLowerCase().replace(name[0], name[0].toUpperCase())
    // console.log('组件名name==>', name)
    // console.log('组件路径==》', item.component)
    const menu = {
      path: '/' + item.route,
      component: item.component,

      // hidden:item.level==1? item.hidden || false,
      // redirect: item.redirect || 'noRedirect',
      children: [],
      name: name,
      // hidden: item.is_menu != 1,
      meta: { title: item.title, icon: item.icon }
      // meta: { title: item.name, id: item.id, roles: ['admin'] }
    }
    if (item.children) {
      generaMenu(menu.children, item.children)
    }

    routes.push(menu)
  })
}
//  遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
  asyncRouterMap.forEach(route => {
    if (route.component) {
      if (route.component === 'Layout') {
        route.component = Layout
      } else {
        try {
          route.component = _import(route.component) // 导入组件
        } catch (error) {
          route.component = _import('system/menu/index') // 导入组件
          Message.error('请查看控制台修改或者删除不存在的组件路径') // 弹出异常('请修改或者删除不存在的组件路径')
        }
      }
    }

    if (route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children)
    }
    // return true
  })
  // console.log(accessedRouters)
  // return accessedRouters
}
